//
//  NewFeatureViewController.swift
//  ydy_weibo
//
//  Created by 杨德勇 on 16/7/18.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let imgcount = 4

class NewFeatureViewController: UICollectionViewController {
    
     init(){
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        
        //设置分页滚动
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

       
    }

    
    //MARK:-
    //MARK:数据源

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
       
        return 1
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return imgcount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.index = indexPath.item
        
    
    
        return cell
    }
    
    //MARK:-
    //MARK:代理方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / screenW)
        
        if page == imgcount - 1 {
            if let cell = collectionView?.visibleCells().last as? NewFeatureCell{
                cell.startAnimation()
                
                
            }
            
        }
    }

}


//MARK:-
//MARK:自定义cell类
class NewFeatureCell:UICollectionViewCell{
    //定义一个属性来帮助cell更换图片
    var index : Int = 0 {
        didSet{
            iconImg.image = UIImage(named: "new_feature_\(index + 1)")
            startBtn.hidden = true
        }
    }
    
    
    @objc private func startBtnDidClick(){
        
        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
        
    }
    //执行动画
    private func startAnimation(){
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { 
            self.startBtn.hidden = false
            self.startBtn.transform = CGAffineTransformMakeScale(1, 1)
            }) { (_) in
                print("动画完成")
        }
        
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        contentView.addSubview(iconImg)
        contentView.addSubview(startBtn)
        
        //设置约束
        iconImg.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView.snp_edges)
        }
        startBtn.snp_makeConstraints { (make) in
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
            make.centerX.equalTo(contentView.snp_centerX)
        }
        
        //设置点击事件
        startBtn.addTarget(self, action: #selector(self.startBtnDidClick), forControlEvents: .TouchUpInside)
        
    }
    //实力化子视图控件
    private lazy var iconImg: UIImageView = UIImageView()
    private lazy var startBtn: UIButton = UIButton(title: "开始体验", backimg: "new_feature_finish_button", color: nil)
}

